﻿using System;
using System.Web.Mvc;
using Business;
using Dto;

namespace YaoYunDong.Controllers
{
    public class UsersController : Controller
    {
        private readonly UserBusiness userBusiness = new UserBusiness();

        public ActionResult UserRegister(string userEmail, string nickName, string sex, string pwd, string rePwd,
                                         string validate)
        {
            if (validate == Session["ValidateNumber"].ToString())
            {
                var userDto = new UsersDto
                                  {
                                      UserEmail = userEmail,
                                      UserId = 0,
                                      UserName = nickName,
                                      UserPwd = pwd,
                                      UserTime = DateTime.Now,
                                      UserType = 1
                                  };
                bool b = userBusiness.Register(userDto);
                if (b)
                {
                    Response.Write("<script>alert('注册成功！');location.href='" + Url.Action("Index", "Home") +
                                   "';</script>");
                    Response.End();
                }
            }
            else
            {
                Response.Write("<script>alert('验证码输入错误');location.href='" + Url.Action("Register", "Home") +
                               "';</Script>");
                Response.End();
            }
            return RedirectToAction("Index", "Home");
        }

        public ActionResult Login(string email, string pwd, string returnPath)
        {
            var dto = userBusiness.Login(email, pwd);
            Session["User"] = dto;
            return Redirect(returnPath);
        }
    }
}